<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background-color: #f5f7fa;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            width: 100%;
            max-width: 400px;
        }

        .login-card {
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            padding: 30px;
            text-align: center;
        }

        .logo {
            color: #3498db;
            font-size: 28px;
            font-weight: 700;
            margin-bottom: 10px;
        }

        .subtitle {
            color: #7f8c8d;
            margin-bottom: 25px;
            font-size: 14px;
        }

        .form-group {
            margin-bottom: 20px;
            text-align: left;
            position: relative;
        }

        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: #2c3e50;
        }

        input {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 14px;
            transition: border 0.3s;
        }

        input:focus {
            border-color: #3498db;
            outline: none;
        }

        .input-hint {
            position: absolute;
            right: 10px;
            top: 50%;
            transform: translateY(-50%);
            background-color: #f8f9fa;
            color: #6c757d;
            font-size: 12px;
            padding: 2px 8px;
            border-radius: 10px;
            border: 1px solid #e9ecef;
            opacity: 0;
            transition: opacity 0.3s, transform 0.3s;
            pointer-events: none;
        }

        input:focus+.input-hint {
            opacity: 1;
            transform: translateY(-50%) translateX(-5px);
        }

        .agreement {
            display: flex;
            align-items: center;
            margin-bottom: 20px;
            font-size: 14px;
        }

        .agreement input {
            width: auto;
            margin-right: 8px;
        }

        .agreement a {
            color: #3498db;
            text-decoration: none;
        }

        .agreement a:hover {
            text-decoration: underline;
        }

        .btn {
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 5px;
            padding: 12px 20px;
            width: 100%;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: background-color 0.3s;
            margin-bottom: 20px;
        }

        .btn:hover {
            background-color: #2980b9;
        }

        @media (max-width: 480px) {
            .login-card {
                padding: 20px;
            }

            .input-hint {
                font-size: 10px;
                padding: 1px 6px;
            }
        }

        .verifyUsername,
        .verifyPassword,
        .verifyAgain,
        .verifyEmail {
            position: absolute;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="login-card">
            <div class="logo">用户注册</div>
            <p class="subtitle">创建您的新账户</p>

            <form class="userReg" id="loginForm" method="post" action="/reg">
                <div class="form-group">
                    <label for="username">用户名</label>
                    <input type="text" id="username" name="username" placeholder="请输入用户名">
                    <span class="input-hint">3-20个字符</span>
                    <span class="verifyUsername"></span>
                </div>

                <div class="form-group">
                    <label for="password">密码</label>
                    <input type="password" id="password" name="password" placeholder="请输入密码">
                    <span class="input-hint">6-20个字符</span>
                    <span class="verifyPassword"></span>
                </div>

                <div class="form-group">
                    <label for="confirmPassword">确认密码</label>
                    <input type="password" id="confirmPassword" name="again" placeholder="请再次输入密码">
                    <span class="input-hint">两次密码需一致</span>
                    <span class="verifyAgain"></span>
                </div>

                <div class="form-group">
                    <label for="email">电子邮箱</label>
                    <input type="email" id="email" name="email" placeholder="请输入电子邮箱">
                    <span class="input-hint">仅支持使用QQ邮箱</span>
                    <span class="verifyEmail"></span>
                </div>

                <div class="agreement">
                    <input type="checkbox" id="agree" name="agree" required>
                    <label for="agree">我已阅读并同意<a href="#">《用户协议》</a></label>
                </div>

                <button type="submit" class="btn">注册</button>
            </form>
        </div>
    </div>
    <script>
        //声明表单验证函数-用户名
        function verifyUsername() {
            if (/^\w{3,20}$/.test(document.querySelector('[name=username]').value)) {
                document.querySelector('.verifyUsername').innerHTML = '✔ 验证通过'
                document.querySelector('.verifyUsername').style.color = 'green'
                document.querySelector('.verifyUsername').style.top = '70px'
                document.querySelector('.verifyUsername').style.right = '1px'
                //返回true
                return true
            }
            else {
                document.querySelector('.verifyUsername').innerHTML = '请输入3-20位字符'
                document.querySelector('.verifyUsername').style.color = 'red'
                document.querySelector('.verifyUsername').style.top = '70px'
                document.querySelector('.verifyUsername').style.right = '1px'
                //返回false
                return false
            }
        }
        //声明表单验证函数-密码
        function verifyPassword() {
            if (/^\w{6,20}$/.test(document.querySelector('[name=password]').value)) {
                document.querySelector('.verifyPassword').innerHTML = '✔ 验证通过'
                document.querySelector('.verifyPassword').style.color = 'green'
                document.querySelector('.verifyPassword').style.top = '70px'
                document.querySelector('.verifyPassword').style.right = '1px'
                //返回true
                return true
            }
            else {
                document.querySelector('.verifyPassword').innerHTML = '请输入6-20位字符'
                document.querySelector('.verifyPassword').style.color = 'red'
                document.querySelector('.verifyPassword').style.top = '70px'
                document.querySelector('.verifyPassword').style.right = '1px'
                //返回false
                return false
            }
        }
        //声明表单验证函数-再次输入密码
        function verifyAgain() {
            if (document.querySelector('[name=again]').value === document.querySelector('[name=password]').value) {
                document.querySelector('.verifyAgain').innerHTML = '✔ 验证通过'
                document.querySelector('.verifyAgain').style.color = 'green'
                document.querySelector('.verifyAgain').style.top = '70px'
                document.querySelector('.verifyAgain').style.right = '1px'
                //返回true
                return true
            }
            else {
                document.querySelector('.verifyAgain').innerHTML = '两次密码不一致'
                document.querySelector('.verifyAgain').style.color = 'red'
                document.querySelector('.verifyAgain').style.top = '70px'
                document.querySelector('.verifyAgain').style.right = '1px'
                //返回false
                return false
            }
        }
        //声明表单验证函数-电子邮箱
        function verifyEmail() {
            if (/^[1-9][0-9]{4,}@qq\.com$/.test(document.querySelector('[name=email]').value)) {
                document.querySelector('.verifyEmail').innerHTML = '✔ 验证通过'
                document.querySelector('.verifyEmail').style.color = 'green'
                document.querySelector('.verifyEmail').style.top = '70px'
                document.querySelector('.verifyEmail').style.right = '1px'
                //返回true
                return true
            }
            else {
                document.querySelector('.verifyEmail').innerHTML = '请输入有效的QQ邮箱'
                document.querySelector('.verifyEmail').style.color = 'red'
                document.querySelector('.verifyEmail').style.top = '70px'
                document.querySelector('.verifyEmail').style.right = '1px'
                //返回false
                return false
            }
        }
        //表单验证-用户名
        document.querySelector('[name=username]').addEventListener('change', verifyUsername)
        //表单验证-密码
        document.querySelector('[name=password]').addEventListener('change', verifyPassword)
        //表单验证-再次输入密码
        document.querySelector('[name=again]').addEventListener('change', verifyAgain)
        //表单验证-电子邮箱
        document.querySelector('[name=email]').addEventListener('change', verifyEmail)

        //表单提交验证模块
        document.querySelector('.userReg').addEventListener('submit', function (e) {
            //1. 如果用户名不符合规范，则无法通过
            if (verifyUsername() === false) {
                //阻止表单默认行为(阻止表单发送http请求)
                e.preventDefault()
                //提醒用户
                return alert('用户名不符合规范')
            }
            //2. 如果密码不符合规范，则无法通过
            if (verifyPassword() === false) {
                //阻止表单默认行为(阻止表单发送http请求)
                e.preventDefault()
                //提醒用户
                return alert('密码不符合规范')
            }
            //3. 如果两次密码不一致，则无法通过
            if (verifyAgain() === false) {
                //阻止表单默认行为(阻止表单发送http请求)
                e.preventDefault()
                //提醒用户
                return alert('两次密码不一致')
            }
            //4. 如果电子邮箱不符合规范，则无法通过
            if (verifyEmail() === false) {
                //阻止表单默认行为(阻止表单发送http请求)
                e.preventDefault()
                //提醒用户
                return alert('电子邮箱不符合规范')
            }
        })
    </script>
</body>

</html>